using System;
using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class HeadBlendSubMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.hbs_Menu, new Point(0, -107));
    private static List<dynamic> faceIdList = new List<object>(new string[1] { string.Empty });
    private static List<dynamic> mixList = new List<object>(new string[1] { string.Empty });
    private static List<dynamic> parentList = new List<object>(new string[2] { "False", "True" });
    private static UIMenuListItem shapeFirstItem;
    private static UIMenuListItem shapeSecondItem;
    private static UIMenuListItem shapeThirdItem;
    private static UIMenuListItem skinFirstItem;
    private static UIMenuListItem skinSecondItem;
    private static UIMenuListItem skinThirdItem;
    private static UIMenuListItem shapeMixItem;
    private static UIMenuListItem skinMixItem;
    private static UIMenuListItem thirdMixItem;
    private static UIMenuListItem parentItem;

    public HeadBlendSubMenu()
    {
        Main.pool.Add(menu);
        menu.OnListChange += OnListChange;
        menu.OnItemSelect += OnItemSelect;
        menu.OnMenuClose += OnMenuClose;
    }

    public static void InitHeadBlendSubMenu()
    {
        menu.Clear();
        faceIdList = new List<object>();
        mixList = new List<object>();
        Main.P1.skin.GetHeadBlendFaceNumber();
        for (int i = 0; i < Main.P1.skin.Head_Blend.faceNumber; i++) faceIdList.Add(i);
        for (double num = 0.0; num <= 1.0; num += 0.1) mixList.Add(Math.Round(num, 1));
        Skin.HeadBlend HeadBlend = (DressingScript.Dressing == Main.Member.P2) ? Main.P2.skin.Head_Blend : Main.P1.skin.Head_Blend;
        menu.SetBannerType(new UIResRectangle());
        shapeFirstItem = new UIMenuListItem(TextSetting.hbs_ListItem_ShapeFirst, faceIdList, HeadBlend.shapeFirstID);
        shapeSecondItem = new UIMenuListItem(TextSetting.hbs_ListItem_ShapeSecond, faceIdList, HeadBlend.shapeSecondID);
        shapeThirdItem = new UIMenuListItem(TextSetting.hbs_ListItem_ShapeThird, faceIdList, HeadBlend.shapeThirdID);
        skinFirstItem = new UIMenuListItem(TextSetting.hbs_ListItem_SkinFirst, faceIdList, HeadBlend.skinFirstID);
        skinSecondItem = new UIMenuListItem(TextSetting.hbs_ListItem_SkinSecond, faceIdList, HeadBlend.skinSecondID);
        skinThirdItem = new UIMenuListItem(TextSetting.hbs_ListItem_SkinThird, faceIdList, HeadBlend.skinThirdID);
        shapeMixItem = new UIMenuListItem(TextSetting.hbs_ListItem_ShapeMix, mixList, mixList.FindIndex(n => n.ToString() == HeadBlend.shapeMix.ToString()));
        skinMixItem = new UIMenuListItem(TextSetting.hbs_ListItem_SkinMix, mixList, mixList.FindIndex(n => n.ToString() == HeadBlend.skinMix.ToString()));
        thirdMixItem = new UIMenuListItem(TextSetting.hbs_ListItem_ThirdMix, mixList, mixList.FindIndex(n => n.ToString() == HeadBlend.thirdMix.ToString()));
        parentItem = new UIMenuListItem(TextSetting.hbs_ListItem_Parent, parentList, Convert.ToInt32(HeadBlend.isParent));
        menu.AddItem(shapeFirstItem);
        menu.AddItem(shapeSecondItem);
        menu.AddItem(shapeThirdItem);
        menu.AddItem(skinFirstItem);
        menu.AddItem(skinSecondItem);
        menu.AddItem(skinThirdItem);
        menu.AddItem(shapeMixItem);
        menu.AddItem(skinMixItem);
        menu.AddItem(thirdMixItem);
        menu.AddItem(parentItem);
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Randomize));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Clear));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
        menu.RefreshIndex();
        menu.Visible = true;
    }

    private static void UpdateHeadBlendValue()
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            Main.P2.skin.Head_Blend.shapeFirstID = shapeFirstItem.Index;
            Main.P2.skin.Head_Blend.shapeSecondID = shapeSecondItem.Index;
            Main.P2.skin.Head_Blend.shapeThirdID = shapeThirdItem.Index;
            Main.P2.skin.Head_Blend.skinFirstID = skinFirstItem.Index;
            Main.P2.skin.Head_Blend.skinSecondID = skinSecondItem.Index;
            Main.P2.skin.Head_Blend.skinThirdID = skinThirdItem.Index;
            Main.P2.skin.Head_Blend.shapeMix = (float)mixList[shapeMixItem.Index];
            Main.P2.skin.Head_Blend.skinMix = (float)mixList[skinMixItem.Index];
            Main.P2.skin.Head_Blend.thirdMix = (float)mixList[thirdMixItem.Index];
            Main.P2.skin.Head_Blend.isParent = (parentItem.Index != 0) ? true : false;
        }
        else
        {
            Main.P1.skin.Head_Blend.shapeFirstID = shapeFirstItem.Index;
            Main.P1.skin.Head_Blend.shapeSecondID = shapeSecondItem.Index;
            Main.P1.skin.Head_Blend.shapeThirdID = shapeThirdItem.Index;
            Main.P1.skin.Head_Blend.skinFirstID = skinFirstItem.Index;
            Main.P1.skin.Head_Blend.skinSecondID = skinSecondItem.Index;
            Main.P1.skin.Head_Blend.skinThirdID = skinThirdItem.Index;
            Main.P1.skin.Head_Blend.shapeMix = (float)mixList[shapeMixItem.Index];
            Main.P1.skin.Head_Blend.skinMix = (float)mixList[skinMixItem.Index];
            Main.P1.skin.Head_Blend.thirdMix = (float)mixList[thirdMixItem.Index];
            Main.P1.skin.Head_Blend.isParent = (parentItem.Index != 0) ? true : false;
            Main.Clone.skin.Head_Blend = Main.P1.skin.Head_Blend;
        }
    }

    private static void OnListChange(UIMenu sender, UIMenuListItem list, int index)
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            SkinHelper.SetPedHeadBlendData(Main.P2.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
            Main.P2.skin.Head_Blend.action = true;
            return;
        }
        SkinHelper.SetPedHeadBlendData(Main.P1.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
        SkinHelper.SetPedHeadBlendData(Main.Clone.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
        Main.P1.skin.Head_Blend.action = true;
        Main.Clone.skin.Head_Blend.action = true;
    }

    private static void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
    {
        switch (index)
        {
            case 10:
                {
                    Random random = new Random(Guid.NewGuid().GetHashCode());
                    shapeFirstItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
                    shapeSecondItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
                    shapeThirdItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
                    skinFirstItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
                    skinSecondItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
                    skinThirdItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
                    shapeMixItem.Index = random.Next(0, mixList.Count);
                    skinMixItem.Index = random.Next(0, mixList.Count);
                    thirdMixItem.Index = random.Next(0, mixList.Count);
                    if (DressingScript.Dressing == Main.Member.P2)
                    {
                        SkinHelper.SetPedHeadBlendData(Main.P2.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
                        Main.P2.skin.Head_Blend.action = true;
                        break;
                    }
                    SkinHelper.SetPedHeadBlendData(Main.P1.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
                    SkinHelper.SetPedHeadBlendData(Main.Clone.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
                    Main.P1.skin.Head_Blend.action = true;
                    Main.Clone.skin.Head_Blend.action = true;
                    break;
                }
            case 11:
                {
                    shapeFirstItem.Index = 0; shapeSecondItem.Index = 0; shapeThirdItem.Index = 0;
                    skinFirstItem.Index = 0; skinSecondItem.Index = 0; skinThirdItem.Index = 0;
                    shapeMixItem.Index = 0; skinMixItem.Index = 0; thirdMixItem.Index = 0; parentItem.Index = 0;
                    if (DressingScript.Dressing == Main.Member.P2)
                    {
                        Main.P2.skin.Head_Blend = default(Skin.HeadBlend);
                        SkinHelper.SetPedHeadBlendData(Main.P2.Character, 0, 0, 0, 0, 0, 0, 0f, 0f, 0f, false);
                    }
                    else
                    {
                        Main.P1.skin.Head_Blend = default(Skin.HeadBlend);
                        SkinHelper.SetPedHeadBlendData(Main.P1.Character, 0, 0, 0, 0, 0, 0, 0f, 0f, 0f, false);
                        Main.Clone.skin.Head_Blend = default(Skin.HeadBlend);
                        SkinHelper.SetPedHeadBlendData(Main.Clone.Character, 0, 0, 0, 0, 0, 0, 0f, 0f, 0f, false);
                    }
                    break;
                }
            case 12:
                DressingScript.Distance();
                break;
            case 13:
                DressingScript.TurnLeft();
                break;
            case 14:
                DressingScript.TurnRight();
                break;
            case 15:
                DressingScript.AngleUp();
                break;
            case 16:
                DressingScript.AngleDown();
                break;
        }
    }

    private static void OnMenuClose(UIMenu sender)
    {
        // 关闭时不做状态写回；变更已在 OnListChange/OnItemSelect 生效
        menu.Visible = false;
        // 不调用上级检查、不刷新，避免退格链式调用
    }
}


